package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album/category")
@SuppressWarnings({"all"})
public class BaseCategoryApiController {
	
	@Autowired
	private BaseCategoryService baseCategoryService;

	/**
	 * 获取分类列表
	 * @return
	 */

	@Operation(summary = "获取分类列表")
	@GetMapping("/getBaseCategoryList")
	public Result getBaseCategoryList() {
		/*List<Param>其中Param自定义
		//Map 也可替换实体类 JSONObject
		例如: class Param{
				private String categoryName;
				private Long categoryId;
				private List<Param> categoryChild;
				}
				*/
		//调用服务层方法
		List<JSONObject> badgeCategoryList = baseCategoryService.getBaseCategoryList();

		//返回值
		return Result.ok(badgeCategoryList);
	}

	/**
	 * 根据一级分类ID查询标签
	 * @param categoryId
	 * @return
	 */
	//http://192.168.200.1/api/album/category/findAttribute/1
	//取值方法一：springmvc /findAttribute/{category1Id} @PathVariable Long category1Id
	//取值方法二：springmvc /findAttribute?category1Id  @RequestParam Long category1Id
	//@RequestParam为Mybaits中方法可以去掉
	@Operation(summary = "根据一级分类ID查询标签")
	@GetMapping("/findAttribute/{category1Id}")
	public Result findAttribute(@PathVariable Long category1Id){
		//调用服务层方法
		List<BaseAttribute>  baseAttributeList = baseCategoryService.findAttribute(category1Id);
		//返回数据
		return Result.ok(baseAttributeList);

	}
	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	/**
	 * 根据三级分类Id 获取到分类数据
	 * @param category3Id
	 * @return
	 */
	@Operation(summary = "根据三级分类Id 获取到分类数据")
	@GetMapping("/getCategoryView/{category3Id}")
	@GuiGuCache(prefix = "category:view:")
	public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id){
		//  select * from base_category_view where id = ?;
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		//  返回数据
		return Result.ok(baseCategoryView);
	}

	/**
	 * 根据一级分类Id获取分类数据
	 * @param category1Id
	 * @return
	 */
	@Operation(summary = "根据一级分类Id获取分类数据")
	@GetMapping("/getBaseCategoryList/{category1Id}")
	public Result getBaseCategoryList(@PathVariable Long category1Id){
		//  调用服务层方法.
		JSONObject category1 = this.baseCategoryService.getBaseCategoryList(category1Id);
		//  返回数据
		return Result.ok(category1);

	}

	/**
	 * 根据一级分类Id查询频道页三级分类列表
	 * @param category1Id
	 * @return
	 */
	@Operation(summary = "根据一级分类Id查询频道页三级分类列表")
	@GetMapping("findTopBaseCategory3/{category1Id}")
	public Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable Long category1Id){
		//已经知道一级分类Id；获取到二级分类Id集合
		List<BaseCategory3> baseCategory3List = baseCategoryService.getBaseCategory3List(category1Id);
		//返回数据
		return Result.ok(baseCategory3List);
	}

	/**
	 * 获取全部一级分类
	 * @return
	 */
	@Operation(summary = "获取全部一级分类")
	@GetMapping("/findAllCategory1")
	public Result<List<BaseCategory1>> findAllCategory1(){
		//  调用服务层方法
		List<BaseCategory1> baseCategory1List = baseCategoryService.findAllCategory1();
		//  返回数据
		return Result.ok(baseCategory1List);
	}


}

